package services

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"ruoyi-go/app/admin/dao"
	"ruoyi-go/app/admin/entity"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/utils/R"
	"strconv"
)

func AlarmInfoSave(context *gin.Context) {
	userId, _ := context.Get("userId")
	log.Println(userId)
	var req entity.AlarmInfo
	if err := context.ShouldBindJSON(&req); err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := dao.AlarmInfoSave(req)
		context.JSON(http.StatusOK, res)
	}
}

func AlarmInfoDelete(context *gin.Context) {
	var id = context.Param("id")
	result := dao.AlarmInfoDelete(id)
	context.JSON(http.StatusOK, result)
}

func AlarmInfoDeleteByIds(context *gin.Context) {
	var ids = context.Param("ids")
	result := dao.AlarmInfoDelete(ids)
	context.JSON(http.StatusOK, result)
}

func AlarmInfoUpdate(context *gin.Context) {
	var req entity.AlarmInfo
	if err := context.ShouldBindJSON(&req); err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := dao.AlarmInfoUpdate(req)
		context.JSON(http.StatusOK, res)
	}
}

func AlarmInfoLoadById(context *gin.Context) {
	idstr := context.Param("id") //查询路径Path参数
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := dao.AlarmInfoLoadById(id)
		context.JSON(http.StatusOK, res)
	}
}

func GetPageLimitAlarmInfoService(context *gin.Context) {
	pageNum, _ := strconv.Atoi(context.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(context.DefaultQuery("pageSize", "10"))

	beginTime := context.DefaultQuery("params[beginTime]", "")
	endTime := context.DefaultQuery("params[endTime]", "")

	var req entity.AlarmInfoPageReq
	// 将查询参数绑定到结构体
	if err := context.ShouldBindQuery(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var param = tools.SearchTableDataParam{
		PageNum:  pageNum,
		PageSize: pageSize,
		Other:    req,
		Params: tools.Params{
			BeginTime: beginTime,
			EndTime:   endTime,
		},
	}
	result := dao.GetPageLimitAlarmInfoService(param)
	context.JSON(http.StatusOK, result)
}
